﻿using System;
using System.Collections.Generic;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Config;
using System.Globalization;
using NTTDATA.BEENChina.ResultCertification.Util;

namespace NTTDATA.BEENChina.ResultCertification.Split
{
    /// <summary>
    /// 年号/年/月/日 项目补充类 为了比较
    /// /25/9/20   > 4/25/9/20  补充年号
    /// /2013/9/20 > 4/25/9/20  公历 年 转换成 和历 
    /// 
    /// 20130927 元号不补4
    /// </summary>
    public class DateHelper
    {
        private CategoryConfig _categoryConfig = null;

        public DateHelper(CategoryConfig categoryConfig)
        {
            _categoryConfig = categoryConfig;
        }

        /// <summary>
        /// 公历 变换 和历
        /// </summary>
        /// <param name="strList"></param>
        /// <returns></returns>
        public  string  ChangeDateStr( string  strList)
        {
            if (strList == null)
            {
                return strList;
            }
            List<string> dateFormatParts = this._categoryConfig.DataFormat.Split(new char[] { '/' }).ToList();
          
            int yyIndex = dateFormatParts.IndexOf("yy");
            int ggIndex = dateFormatParts.IndexOf("gg");

            if (yyIndex == -1)
            {
                yyIndex = dateFormatParts.IndexOf("yyyy");
                
            }

            if (yyIndex > -1)
            {
                strList  = ChangeYear(yyIndex,ggIndex, strList );
            }
            
            return strList;
        }

        /// <summary>
        /// 公历 变换 和历
        /// </summary>
        /// <param name="strList"></param>
        /// <returns></returns>
        public List<string> ChangeDateStr(List<string> strList)
        {
            if (strList == null)
            {
                return strList;
            }
           
            for (int i = 0; i < strList.Count; i++)
            {

                strList[i] = ChangeDateStr( strList[i]);
                
            }
            return strList;
        }

/*         private string ChangeGg(int GgIndex, string str)
         {
             string[] strParts = str.Split(new char[] { '/' });
             if (strParts == null)
             {
                 return str;
             }

             if (strParts.Count() <= GgIndex)
             {
                 return str;
             }

             string ggStr = strParts[GgIndex];
             if (string.IsNullOrEmpty(ggStr ))
             {
                 strParts[GgIndex] = "4";//平成
             }

             string splitFormat = _categoryConfig.SplitFormat.Split(new string[] { @"\\" }, StringSplitOptions.None)[0];

             return string.Format(splitFormat, strParts);            
         }*/

        private string ChangeYear(int yearIndex,int ggIndex, string str)
        {
            string[] strParts = str.Split(new char[] { '/' }); 
            
            if (strParts.Count() <= yearIndex)
            {
                return str;
            }

           

            string yyStr = strParts[yearIndex];

            string[] curYears =new string[]{ "’","'"};
            //年的录入结果中有 ‘
            //如果是 '25  ->  4/25
            //如果是 '13 -> 2013
            if ( curYears.Any( yyStr.Contains))
            {
                string splitChar = curYears.Where(yyStr.Contains).Single();
                string tempYear = yyStr.Replace(splitChar, "20");

                if (System.String.CompareOrdinal(tempYear, DateTime.Now.Year.ToString(CultureInfo.InvariantCulture)) >= 0)
                {
                    if ( ggIndex > -1) 
                    {
                        strParts[ggIndex] = "4";
                    }
                    yyStr = strParts[yearIndex] = yyStr.Replace(splitChar, string.Empty);
                }
                else
                {
                    yyStr = strParts[yearIndex] = yyStr.Replace(splitChar, "20");
                }
                
            }

            if (yyStr.Length == 4 && String.CompareOrdinal(yyStr, "1868") < 0)
            {
                Console.WriteLine("yy err");
            }



            if (yyStr.Length == 4 &&  String.CompareOrdinal(yyStr, "1868")>0 && StrHelper.IsNumeric(yyStr) )
            {
                /*
                CultureInfo ci = new CultureInfo("ja-JP");
                Calendar cal = new JapaneseCalendar();
                ci.DateTimeFormat.Calendar = cal;
                DateTime dt = DateTime.Parse(yyStr + "/01/01");
                
                strParts[yearIndex] = dt.ToString("yy", ci);

                //4位的年变成和历后
                //相应的元号也跟着变更
                if (ggIndex > -1)
                {
                    strParts[ggIndex] = cal.GetEra(dt).ToString(CultureInfo.InvariantCulture); ;

                   
                }
                 */

                var gann = Util.DateStrHelper.GetGannnennB(yyStr);
                
                if (ggIndex > -1)
                {
                    strParts[yearIndex] = gann["yy"];
                    strParts[ggIndex] = gann["gg"];
                }
            }

            //4位年 2位结果
            //年补充位数
            if (_categoryConfig.DataFormat.Contains("yyyy") && strParts[yearIndex].Length.Equals(2))
            {
                strParts[yearIndex] = string.Format("19{0}", strParts[yearIndex]);
            }
            string splitFormat = _categoryConfig.SplitFormat.Split(new string[] { @"\\" }, StringSplitOptions.None)[0];
            return string.Format(splitFormat, strParts);     

//            return string.Format(_categoryConfig.SplitFormat, strParts);     
        }



        /// <summary>
        /// 补充日期
        /// 元号 4
        /// 年 24
        /// 月 1
        /// 日 1
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public string CompleteDate(string date)
        {

            List<string> dateFormatParts = _categoryConfig.DataFormat.Split(new char[] { '/' }).ToList();

            int ggIndex = dateFormatParts.IndexOf("gg");
            int yyIndex = dateFormatParts.IndexOf("yy");
            if (yyIndex == -1)
            {
                yyIndex = dateFormatParts.IndexOf("yyyy");
            }

            var rowArr = date.Split(new char[] { '/' });

    

            if (ggIndex > -1)
            {
                if (String.IsNullOrEmpty(rowArr[ggIndex]))
                {
                    rowArr[ggIndex] = "4";//平成
                }
 
            }

            if (yyIndex > -1)
            {
                if (String.IsNullOrEmpty(rowArr[yyIndex]))
                {
                    rowArr[yyIndex] = "24"; 
                }

 

            }

            rowArr = rowArr.Select(tt => string.IsNullOrEmpty(tt) ? "01" : tt).ToArray();

            return string.Join("/", rowArr);

        }

    }
}
